package jsoup;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import cn.demoncat.util.http.JsoupUtil;
import cn.demoncat.util.io.IoUtil;
import cn.demoncat.util.lang.JsonUtil;
import cn.demoncat.util.lang.PinyinUtil;
import cn.demoncat.util.lang.StringUtil;
import cn.demoncat.util.web.entity.HttpParam;

/**
 * 品牌库搜索
 * 
 * @author 延晓磊
 *
 * @since 2020年6月16日
 */
public class BrandQuery {

	@Test
	public void pp918() throws FileNotFoundException {
		// 品牌之家
		String url = "https://www.pp918.com/brandlist_%s.html";
		int size = 1736;
		List<Brand> brands = new ArrayList<>();
		Brand brand = null;
		for (int i = 1; i < size; i++) {
			// 列表
			Document doc = JsoupUtil.http(HttpParam.get(String.format(url, i)).times(5));
			Elements bs = doc.select(".brand-list li");
			for (Element b : bs) {
				// 品牌
				brand = new Brand();
				brand.setLogo(StringUtils.trim(b.select(".bdlis-logo img").get(0).absUrl("src")));
				brand.setName(b.getElementsByTag("tr").get(0).getElementsByTag("a").get(0).text().trim());
				brand.setInitial(StringUtil.get(PinyinUtil.toPinyin(brand.getName()), 0));
				brands.add(brand);
			}
		}
		IoUtil.write(JsonUtil.toFormatStr(brands), new FileOutputStream("E:/brand-pp918.json"));
	}
	
	
	@Test
	public void maizhi() throws FileNotFoundException {
		// 麦知网
		String url = "http://lc.maizhi.com/ajax_index_brand/%s";
		int size = 817;
		List<Brand> brands = new ArrayList<>();
		Brand brand = null;
		for (int i = 1; i < size; i++) {
			// 列表
			Document doc = JsoupUtil.http(HttpParam.get(String.format(url, i)).times(5));
			Elements bs = doc.select("li");
			for (Element b : bs) {
				// 品牌
				brand = new Brand();
				try {
					brand.setLogo(StringUtils.trim(b.getElementsByClass("img-container").get(0).getElementsByTag("img").get(0).absUrl("src")));
				} catch (Exception e) {
					// 广告
					continue;
				}
				brand.setName(b.getElementsByClass("brand_p1").get(0).text().trim());
				brand.setInitial(StringUtil.get(PinyinUtil.toPinyin(brand.getName()), 0));
				brands.add(brand);
			}
		}
		IoUtil.write(JsonUtil.toFormatStr(brands), new FileOutputStream("E:/brand-maizhi.json"));
	}
	
	@Test
	public void huajx() throws FileNotFoundException {
		// 化工机械网
		String url = "http://www.huajx.com/brands/t0/list_p%s.html";
		int size = 77;
		List<Brand> brands = new ArrayList<>();
		Brand brand = null;
		for (int i = 1; i < size; i++) {
			// 列表
			Document doc = JsoupUtil.http(HttpParam.get(String.format(url, i)).times(5));
			Elements bs = doc.select(".pptu dl");
			for (Element b : bs) {
				// 品牌
				brand = new Brand();
				brand.setLogo(StringUtils.trim(b.child(0).getElementsByTag("img").get(0).absUrl("src")));
				brand.setName(b.child(1).child(0).text().trim());
				brand.setInitial(StringUtil.get(PinyinUtil.toPinyin(brand.getName()), 0));
				brands.add(brand);
			}
		}
		IoUtil.write(JsonUtil.toFormatStr(brands), new FileOutputStream("E:/brand-huajx.json"));
	}
	
	@Test
	public void zgong() throws FileNotFoundException {
		// 重工机械网
		String url = "http://www.zgong.com/brands/t0/list_p%s.html";
		int size = 35;
		List<Brand> brands = new ArrayList<>();
		Brand brand = null;
		for (int i = 1; i < size; i++) {
			// 列表
			Document doc = JsoupUtil.http(HttpParam.get(String.format(url, i)).times(5));
			Elements bs = doc.select(".pptu dl");
			for (Element b : bs) {
				// 品牌
				brand = new Brand();
				brand.setLogo(StringUtils.trim(b.child(0).getElementsByTag("img").get(0).absUrl("src")));
				brand.setName(b.child(1).child(0).text().trim());
				brand.setInitial(StringUtil.get(PinyinUtil.toPinyin(brand.getName()), 0));
				brands.add(brand);
			}
		}
		IoUtil.write(JsonUtil.toFormatStr(brands), new FileOutputStream("E:/brand-zgong.json"));
	}
	
	/** 
	 * 品牌库
	 *
	 * @author 延晓磊
	 *
	 * @since 2020-06-16
	 */
	public static class Brand {
		// 名称
		private String name;
		// 首字母
		private String initial;
		// 图标
		private String logo;

		public Brand() {
			super();
		}

		public String getName() {
			return this.name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getInitial() {
			return this.initial;
		}

		public void setInitial(String initial) {
			this.initial = initial;
		}

		public String getLogo() {
			return this.logo;
		}

		public void setLogo(String logo) {
			this.logo = logo;
		}
	}
}
